/**
 * 
 */
package com.sx.article.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.sx.article.biz.IArticleBiz;
import com.sx.article.biz.IAttachmentBiz;
import com.sx.article.biz.IKeyWordBiz;
import com.sx.article.biz.ITitleInfoBiz;
import com.sx.article.dao.IArticleDao;
import com.sx.article.entity.ArticleAndAttachmentEntity;
import com.sx.article.entity.ArticleEntity;
import com.sx.article.entity.AttachmentEntity;
import com.sx.article.entity.KeyWordAndArticleUnionEntity;
import com.sx.article.entity.TitleAndArticleEntity;
import com.sx.feesunit.exception.SXException;

/**
 * @author Administrator
 *
 */
public class ArticleBiz implements IArticleBiz{
	private IAttachmentBiz attachmentbiz;
	private IArticleDao articledao;
	private Log log  = LogFactory.getLog(this.getClass());
	private IKeyWordBiz keywordbiz;
	private ITitleInfoBiz titleinfobiz;
	/**
	 *查询我的表单信息列表 
	 */
	public List<ArticleEntity> findArticleInfos(ArticleEntity article) throws SXException{
		List<ArticleEntity> list = new ArrayList<ArticleEntity>();
		try {
			list = articledao.findArticleInfo(article);
		} catch (Exception e) {
			log.error("获取文章数据信息",e);
			throw new SXException(e);
		}
		return list;
	}
	/**
	 * 查询我的表单信息可以进行按照标题获文章内容进行分页查询
	 **/
	public String findArticleInfoToArrayType(ArticleEntity article,String totalkey,String datakey) throws SXException{
		String str = "{\""+totalkey+"\":"+2+",\""+datakey+"\":[]}";
		try {
			String counts = articledao.findArticleInfoCounts(article);
			Integer pagesize = Integer.parseInt(counts)/article.getLimit();
			if(Integer.parseInt(counts)%article.getLimit()!=0){
				pagesize = pagesize+1;
			}
			List<ArticleEntity> list = articledao.findArticleInfo(article);
			Map map = new HashMap();
			map.put(totalkey, "4");
			map.put(datakey, list);
			String data = JSONArray.toJSONString(map);
			str = data;
//			str = "{\""+totalkey+"\":"+pagesize+",\""+datakey+"\":"+data+"}";
		} catch (Exception e) {
			// TODO: handle exception
			log.error("新增文章数据信息",e);
			throw new SXException(e);
		}
		return str;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IArticleBiz#approvalArticleInfo(com.sx.article.entity.ArticleEntity)
	 */
	public boolean approvalArticleInfo(ArticleEntity article) throws SXException {
		boolean flag = false;
		try {
			flag = this.articledao.approvalArticleInfo(article);
		} catch (Exception e) {
			log.error("审核文章数据信息",e);
			// TODO: handle exception
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IArticleBiz#deleteInfo(com.sx.article.entity.ArticleEntity)
	 */
	public boolean deleteInfo(ArticleEntity article) throws SXException {
		boolean flag = false;
		try {
			flag = this.articledao.deleteInfo(article);
			if (flag) {
				Integer id = article.getAid();
				//删除按照文章编号关键字信息
				boolean _deletekeywords = keywordbiz.deleteKeyWordsByArticleId(id);
				
				//按照文章编号删除主题信息
				boolean _deletetitle = titleinfobiz.deleteTitleByArticleId(id);
				
			}
		} catch (Exception e) {
			log.error("删除文章数据信息",e);
			// TODO: handle exception
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IArticleBiz#editInfo(com.sx.article.entity.ArticleEntity)
	 */
	public boolean editInfo(ArticleEntity article) throws SXException{
		boolean flag = false;
		try {
			flag = this.articledao.editInfo(article);
			if (flag) {
				Integer id = article.getAid();
				//删除按照文章编号关键字信息
				boolean _deletekeywords = keywordbiz.deleteKeyWordsByArticleId(id);
				if (_deletekeywords) {
					//添加关键字关联
					Integer [] keywords = article.getKeywords();
					for (Integer keywordid : keywords) {
						KeyWordAndArticleUnionEntity kwe = new KeyWordAndArticleUnionEntity(id,keywordid);
						keywordbiz.addKeyWordAndArticeUnion(kwe);
					}
				}
				//按照文章编号删除主题信息
				boolean _deletetitle = titleinfobiz.deleteTitleByArticleId(id);
				if (_deletetitle) {
					//添加主题信息
					Integer [] topicids = article.getTopicids();
					for (Integer topicid : topicids) {
						TitleAndArticleEntity taae = new TitleAndArticleEntity(topicid,id);
						this.addTitleArticleInfo(taae);
					}
				}
				//添加附件和表单关联
				List<AttachmentEntity> list = article.getAttachment();
				if (list!=null) {
					for (AttachmentEntity attachment : list) {
						ArticleAndAttachmentEntity aaae = new ArticleAndAttachmentEntity(id,attachment.getFid());
						this.attachmentbiz.addAttachmentByArticleId(aaae);
					}
				}
			}
		} catch (Exception e) {
			log.error("编辑文章数据信息",e);
			// TODO: handle exception
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IArticleBiz#findArticleById(com.sx.article.entity.ArticleEntity)
	 */
	public ArticleEntity findArticleById(ArticleEntity article)  throws SXException{
		ArticleEntity arti = null;
		try {
			arti = this.articledao.findArticleById(article);
		} catch (Exception e) {
			log.error("获取文章数据信息",e);
			throw new SXException(e);
		}
		return arti;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IArticleBiz#addTitleArticleInfo(com.sx.article.entity.TitleAndArticleEntity)
	 */
	public Integer addTitleArticleInfo(TitleAndArticleEntity aticleandtitle) throws SXException{
		Integer id = null;
		try {
			id = this.articledao.addTitleArticleInfo(aticleandtitle);
		} catch (Exception e) {
			log.error("新增文章数据信息",e);
			throw new SXException(e);
			// TODO: handle exception
		}
		return id;
	}
	/**
	 * 查询我的表单信息可以进行按照标题获文章内容进行分页查询
	 **/
	public String findArticleInfo(ArticleEntity article) throws SXException{
		String str =  "{totalCount:0,data:[]}";
		try {
			String counts = articledao.findArticleInfoCounts(article);
			List<ArticleEntity> list = articledao.findArticleInfo(article);
			String data = JSONArray.toJSONString(list);
			str = "{totalCount:"+counts+",data:"+data+"}";
		} catch (Exception e) {
			// TODO: handle exception
			log.error("新增文章数据信息",e);
			throw new SXException(e);
		}
		return str;
	}
	/**
	 * 新增文章数据信息
	 **/
	public Integer addInfo(ArticleEntity article) throws SXException{
		Integer id = null;
		try {
			id = articledao.addInfo(article);
			if(id!=null){
				//添加关键字关联
				Integer [] keywords = article.getKeywords();
				if(keywords!=null){
					for (Integer keywordid : keywords) {
						KeyWordAndArticleUnionEntity kwe = new KeyWordAndArticleUnionEntity(id,keywordid);
						keywordbiz.addKeyWordAndArticeUnion(kwe);
					}
				}
				//添加主题信息
				Integer [] topicids = article.getTopicids();
				if (topicids!=null) {
					for (Integer topicid : topicids) {
						TitleAndArticleEntity taae = new TitleAndArticleEntity(topicid,id);
						this.addTitleArticleInfo(taae);
					}
				}
				//添加附件和表单关联
				List<AttachmentEntity> list = article.getAttachment();
				if (list!=null) {
					for (AttachmentEntity attachment : list) {
						ArticleAndAttachmentEntity aaae = new ArticleAndAttachmentEntity(id,attachment.getFid());
						this.attachmentbiz.addAttachmentByArticleId(aaae);
					}
				}
			}
		} catch (Exception e) {
			log.error("新增文章数据信息",e);
			throw new SXException(e);
			// TODO: handle exception
		}
		return id;
	}
	/**
	 * @return the articledao
	 */
	public IArticleDao getArticledao() {
		return articledao;
	}
	/**
	 * @param articledao the articledao to set
	 */
	public void setArticledao(IArticleDao articledao) {
		this.articledao = articledao;
	}
	/**
	 * @return the keywordbiz
	 */
	public IKeyWordBiz getKeywordbiz() {
		return keywordbiz;
	}
	/**
	 * @param keywordbiz the keywordbiz to set
	 */
	public void setKeywordbiz(IKeyWordBiz keywordbiz) {
		this.keywordbiz = keywordbiz;
	}
	/**
	 * @return the titleinfobiz
	 */
	public ITitleInfoBiz getTitleinfobiz() {
		return titleinfobiz;
	}
	/**
	 * @param titleinfobiz the titleinfobiz to set
	 */
	public void setTitleinfobiz(ITitleInfoBiz titleinfobiz) {
		this.titleinfobiz = titleinfobiz;
	}
	/**
	 * @return the attachmentbiz
	 */
	public IAttachmentBiz getAttachmentbiz() {
		return attachmentbiz;
	}
	/**
	 * @param attachmentbiz the attachmentbiz to set
	 */
	public void setAttachmentbiz(IAttachmentBiz attachmentbiz) {
		this.attachmentbiz = attachmentbiz;
	}
	
	
	
}
